package com.zhang.bingmei_incomeexpenditure.service.chain.Impl;

import com.zhang.bingmei_incomeexpenditure.entity.ie.updateNew.UpdateDoubleDTO;
import com.zhang.bingmei_incomeexpenditure.service.chain.ValidationHandler;
import org.apache.commons.lang3.StringUtils;

// 年份合法性校验处理器
public class YearValidationHandler implements ValidationHandler {
    private ValidationHandler next;

    @Override
    public void setNext(ValidationHandler next) {
        this.next = next;
    }

    @Override
    public void validate(UpdateDoubleDTO dto) {
        if (StringUtils.isNotBlank(dto.getYear())) {
            try {
                int year = Integer.parseInt(dto.getYear());
                int currentYear = java.time.LocalDate.now().getYear();
                if (year > currentYear) {
                    throw new RuntimeException("年份不能大于当前年：" + currentYear);
                }
            } catch (NumberFormatException e) {
                throw new RuntimeException("年份格式错误：" + dto.getYear());
            }
        }
        if (next != null) {
            next.validate(dto);
        }
    }
}